/*
 * @Author: wangyifei 844650674@qq.com
 * @Date: 2022-11-27 10:22:27
 * @LastEditors: wangyifei 844650674@qq.com
 * @LastEditTime: 2022-11-27 13:45:55
 * @FilePath: \management_system_template\src\service.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export function ajaxRequest(url, method, params) {
  return new Promise((resolve, reject) => {
    let data;
    if (method === "POST" || method === "PUT") {
      data = JSON.stringify(params);
    } else {
      data = null;
    }
    $.ajax({
      type: method,
      url,
      data,
      contentType: "application/json;charset=utf-8",
      beforeSend: function(request) {
        if (localStorage.getItem("userInfo")) {
          request.setRequestHeader(
            "Authorization",
            JSON.parse(localStorage.getItem("userInfo")).token
          );
        }
      },
      success: (res) => {
        resolve(res);
      },
      error: (jqXHR, textStatus, errorThrown) => {
        console.log(jqXHR.status == 401)
        if (jqXHR.status == 401) {
          alert('登录信息过期，请重新登陆')
        }
        reject(textStatus);
      },
    });
  });
}

// 登录
export const login = (params) => {
  return ajaxRequest("/user/login", "POST", params);
};

// 新建用户
export const addUser = (params) => {
  return ajaxRequest("/user/register", "POST", params);
};

// 获取用户列表
export const getUser = (params) => {
  return ajaxRequest(`/user/get`, "POST", params);
};

// getUserInfo
export const getUserInfo = (id) => {
  return ajaxRequest(`/user/${id}`, "GET");
};

export const hasAdmin = (params) => {
  return ajaxRequest(`/user/hasAdmin`, "POST", params);
}

// 删除用户
export const deleteUser = (id) => {
  return ajaxRequest(`/user/${id}`, "DELETE");
}

// 编辑用户
export const updateUser = (id, params) => {
  return ajaxRequest(`/user/${id}`, "POST", params);
};
export const updateUserP = (id, params) => {
  return ajaxRequest(`/user/pass/${id}`, "POST", params);
};
// 获取模板信息
export const getSetting = () => {
  return ajaxRequest(`/setting`, "GET");
}
// 编辑用户
export const updateSetting = (params) => {
  return ajaxRequest(`/setting`, "POST", params);
};

export const getRecord = () => {
  return ajaxRequest(`/record`, "GET");
}
export const getOrder = (params) => {
  return ajaxRequest(`/order?${params}`, "GET");
}

export const getMenu = (params) => {
  return ajaxRequest(`/menu/get`, "POST", params);
}
export const addMenu = (params) => {
  return ajaxRequest(`/menu/add`, "POST", params);
}
export const updateMenu = (id, params) => {
  return ajaxRequest(`/menu/${id}`, "POST", params);
}
export const deleteMenu = (id) => {
  return ajaxRequest(`/menu/${id}`, "DELETE");
}
export const getOrderByDate = (params) => {
  return ajaxRequest(`/order/all?${params}`, "GET", {});
}
export const getHealth = (params) => {
  return ajaxRequest(`health/get`, "POST", params);
}
export const addOrder = (params) => {
  return ajaxRequest("/order", "POST", params);
};

export const addClose = (params) => {
  return ajaxRequest("/close", "POST", params);
};
export const deleteOrder = (id) => {
  return ajaxRequest(`/order/${id}`, "DELETE");
}

export const updateOrder = (id, params) => {
  return ajaxRequest(`/order/${id}`, "POST", params);
};

export const updateClose = (id, params) => {
  return ajaxRequest(`/close/${id}`, "POST", params);
};

export const addOrderCommon = (params) => {
  return ajaxRequest("/order/common", "POST", params);
};


export const getType = (params) => {
  return ajaxRequest(`/type/get`, "POST", params);
}
export const addType = (params) => {
  return ajaxRequest("/type", "POST", params);
};

export const deleteType = (id) => {
  return ajaxRequest(`/type/${id}`, "DELETE");
}

export const updateType = (id, params) => {
  return ajaxRequest(`/type/${id}`, "POST", params);
};


export const getAction = (params) => {
  return ajaxRequest(`/action/get`, "POST", params);
}
export const addAction = (params) => {
  return ajaxRequest("/action", "POST", params);
};

export const deleteAction = (id) => {
  return ajaxRequest(`/action/${id}`, "DELETE");
}

export const updateAction = (id, params) => {
  return ajaxRequest(`/action/${id}`, "POST", params);
};
